<?php


namespace App\Services;


use Swoole\WebSocket\Server;

class UserLineService
{
    public static function setOnline(object $user, int $fd): void
    {
        $fd = (string)$fd;

        redis()->hSet('im_user_online', $fd, (string)$user->id);
        redis()->hSet('user_id_to_fd', (string)$user->id, $fd);
    }

    public static function setOutline(Server $server, int $fd): void
    {
        $fd = (string)$fd;

        $userId = redis()->hGet('im_user_online', $fd);

        redis()->hDel('user_id_to_fd', $userId);
        redis()->hDel('im_user_online', $fd);

        $server->close($fd);
    }

}